package com.cwx.mychat.openai.controller;

import com.cwx.mychat.entity.constant.CodeConstant;
import com.cwx.mychat.entity.constant.SysSettingConstant;
import com.cwx.mychat.entity.dto.TokenUserInfoDto;
import com.cwx.mychat.entity.enums.ExceptionBizCodeEnum;
import com.cwx.mychat.entity.enums.UserContactTypeEnum;
import com.cwx.mychat.entity.po.ChatMessage;
import com.cwx.mychat.entity.po.ChatSession;
import com.cwx.mychat.entity.vo.MessageSendVo;
import com.cwx.mychat.exception.BusinessException;
import com.cwx.mychat.openai.service.AIChatSendMessage;
import com.cwx.mychat.service.ChatMessageService;
import com.cwx.mychat.service.ChatSessionService;
import com.cwx.mychat.utils.R;
import com.cwx.mychat.utils.common.CommonUtil;
import com.cwx.mychat.utils.redis.RedisComponent;
import com.cwx.mychat.utils.redis.RedisUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.time.Duration;

/**
 * 作者： cwx
 * 时间： 2025/1/2 11:31
 * 版本： 1.0
 * 注释：
 */
@RestController
@RequestMapping("/ai")
public class AIChatMessageController {

    @Resource
    private AIChatSendMessage aiChatSendMessage;

    @Resource
    private RedisComponent redisComponent;

    @Resource
    private ChatMessageService chatMessageService;

    @Resource
    private ChatSessionService chatSessionService;

    /**
     * 桌面应用程序 手机APP通过webSocket返回数据
     * @param chatMessage
     * @return
     * @throws BusinessException
     */
    @PostMapping(value = "/chat")
    public R aiChat(@RequestBody ChatMessage chatMessage) throws BusinessException {
        TokenUserInfoDto tokenUserInfoDto = CommonUtil.getSecurityContext();
        //判断是否是机器人
        SysSettingConstant sysSetting = redisComponent.getSysSetting();
        if(!sysSetting.getRobotUid().equals(chatMessage.getContactId())){
            throw new BusinessException(ExceptionBizCodeEnum.ROBOT_NOT_EXIST);
        }
        //保存信息
        chatMessageService.saveMessage(chatMessage, tokenUserInfoDto);
        //更新session
        ChatSession chatSession = chatSessionService.updateSession(chatMessage, tokenUserInfoDto, UserContactTypeEnum.USER);
        MessageSendVo messageSendVo = new MessageSendVo();
        BeanUtils.copyProperties(chatMessage, messageSendVo);
        messageSendVo.setMessageId(chatMessage.getMessageId().toString());
        messageSendVo.setLastMessage(chatSession.getLastMessage());
        messageSendVo.setLastReceiveTime(chatSession.getLastReceiveTime());
        messageSendVo.setSendTime(chatMessage.getSendTime());
        aiChatSendMessage.sendMessage(chatMessage, tokenUserInfoDto, sysSetting);
        return R.ok().put(CodeConstant.RESULT_MSG, messageSendVo);
    }

    /**
     * 中断流消息操作
     * @return
     */
    @GetMapping("/stop")
    public R interrupt(){
        return aiChatSendMessage.interrupt();
    }

    /**
     * 回流数据
     * @param chatMessage
     * @return
     */
    @PostMapping(value = "/deskTopChat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> deskTopAIChat(@RequestBody ChatMessage chatMessage) throws BusinessException {
        return aiChatSendMessage.sendMessage(chatMessage);
    }
}
